import {
    getBindCompanySupplier,
    createProject
} from '../api/app.js'
import user from '../utils/user.js'
const app = getApp()
import { dateFormat } from '../utils/util.js'
Component({
    data: {
        selected: 0,
        color: '#333',
        selectedColor: '#FF7642',
        list: [{
            'pagePath': '/pages/index/index',
            'text': '首页',
            'iconPath': '../assets/img/tab-bar/home.png',
            'selectedIconPath': '../assets/img/tab-bar/home-active.png'
        },
        {
            'pagePath': '/pages/space/space',
            'text': '我的',
            'iconPath': '../assets/img/tab-bar/my.png',
            'selectedIconPath': '../assets/img/tab-bar/my-active.png'
        }
        ],
        isShow: 1,
        addProjectStatus: false,
        companyList: [],
        enter: '',
        date: '',
        selectStartDate: '',
        addSuccessStatus: false,
        newProjectId: '',
        newProjectName: ''
    },
    ready(){
        this.setData({
            selectStartDate: dateFormat('YYYY-mm-dd', new Date())
        })
    },
    methods: {
        switchTab(e) {
            console.log(e, 'e.currentTarget.dataset')
            // 震动反馈
            wx.vibrateShort({
                type: 'heavy'
            })
            const data = e.currentTarget.dataset
            const url = data.path
            wx.switchTab({
                url
            })
            // this.setData({
            //     selected: data.index
            // })
        },
        bindDateChange(e) {
            this.setData({
                'date': e.detail.value
            })
        },
        tapEnter(event) {
            this.setData({
                'enter': event.currentTarget.dataset.name,
                tipList: []
            })
        },
        enterInput(event) {
            const data = []
            for (const i in this.data.companyList) {
                if (this.data.companyList[i].supplierName.indexOf(event.detail.value) !== -1) {
                    data.push(this.data.companyList[i])
                }
            }

            this.setData({
                'enter': event.detail.value,
                tipList: data
            })
        },
        enterInputConfirm(event){
            this.setData({
                'enter': event.detail.value,
                tipList: []
            })
        },
        addProject(date) {
            console.log(date)
            this.setData({
                addProjectStatus: true,
                date: date.target ? dateFormat('YYYY-mm-dd', new Date()) : date
            })
			
            getBindCompanySupplier({
                companyId: user.companyId,
                type: 0
            }).then((res) => {
                this.setData({
                    'companyList': res.rows
                })
            })
        },
        createProject() {
            if (!this.data.enter) {
                app.toast('请输入企业名称')
                return 0
            }
            if (!this.data.date) {
                app.toast('请选择时间')
                return 0
            }
            createProject({
                companyId: user.companyId,
                supplierName: this.data.enter,
                createUserId: user.userId,
                date: this.data.date
            }).then((res) => {
                this.setData({
                    newProjectName: this.data.enter,
                    enter: '',
                    addSuccessStatus: true,
                    newProjectId: res
                })
                this.cancelAdd()
                this.addCallBack()
            })
        },
        cancelAdd() {
            this.setData({
                addProjectStatus: false
            })
        },
        cancelSuccess(){
            this.setData({
                addSuccessStatus: false
            })
        },
        addProjectCallBack(func){
            this.addCallBack = func
        },
        goInvite(){
            this.cancelSuccess()
            wx.navigateTo({
                url: '/packageA/pages/master-invitation-qrcode/master-invitation-qrcode?projectId=' + this.data.newProjectId + '&projectName=' + this.data.newProjectName
            })
        },
        goProject(){
            this.cancelSuccess()
            wx.navigateTo({
                url: '/packageA/pages/resident/resident?id=' + this.data.newProjectId
            })
        }
    }
})
